package com.eleme.user.controller;

import com.eleme.user.dto.UserLoginDTO;
import com.eleme.user.dto.UserRegisterDTO;
import com.eleme.user.service.UserService;
import com.eleme.user.vo.Result;
import com.eleme.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/UserController")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result<String> register(@Validated @RequestBody UserRegisterDTO dto) {
        return Result.success(userService.register(dto));
    }

    @PostMapping("/login")
    public Result<String> login(@Validated @RequestBody UserLoginDTO dto) {
        return Result.success(userService.login(dto));
    }

    @GetMapping("/list")
    public Result<List<UserVO>> listAll() {
        return Result.success(userService.listAll());
    }

    @GetMapping("/getUserByIdByPass/{userId}/{password}")
    public Result<UserVO> getUserByIdByPass(@PathVariable("userId") String userId, @PathVariable("password") String password) {
        return Result.success(userService.getUserByIdByPass(userId, password));
    }

    @GetMapping("/getUserById/{userId}")
    public Result<Integer> getUserById(@PathVariable("userId") String userId) {
        return Result.success(userService.getUserById(userId));
    }

    @PostMapping("/saveUser/{userId}/{password}/{userName}/{userSex}")
    public Result<Integer> saveUser(@PathVariable("userId") String userId, @PathVariable("password") String password, @PathVariable("userName") String userName, @PathVariable("userSex") Integer userSex) {
        return Result.success(userService.saveUser(userId, password, userName, userSex));
    }

    @PostMapping("/update")
    public Result<String> update(@Validated @RequestBody UserVO vo) {
        return Result.success(userService.updateUser(vo));
    }

    @DeleteMapping("/{userId}")
    public Result<String> delete(@PathVariable String userId) {
        return Result.success(userService.deleteUser(userId));
    }
} 